package com.zerosky.crm.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zerosky.crm.po.Customer;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.Reserve;
import com.zerosky.crm.service.CustomerService;
import com.zerosky.crm.service.ReserveService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.PageInfo;

/**
 * 用户相关业务的controller
 * 
 * @author Administrator
 *
 */
@Controller
public class ReserveController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(ReserveController.class); // 日志

	@Resource(name = "reserveService")
	private ReserveService reserveService;

	@Resource(name = "customerService")
	private CustomerService customerService;


	
	/**
	 * 查询指定员工的预约客户
	 * 
	 * @return
	 */
	@RequestMapping("/getReserveCustomer.do")
	@ResponseBody
	public Map<String,Object> getReserveCustomer(String userId, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) {
		Map<String, Object> json = new HashMap<>();
		Employee user = (Employee) session.getAttribute("loginUser");
		Map<String, Object> map = new HashMap<>();
		try {
			if ("".equals(userId) || userId == null) {
				userId = user.getEmployeeId();
			}
			map.put("personLiable", userId);
			List<Customer> list = customerService.getCustomerList(map);
			json.put("userId", userId);
			json.put("customer", list);
			json.put("code", 0);
		} catch (Exception e) {
			logger.error("获取数据失败，原因：" + e);
			json.put("code", -1);
		}		
		return json;
	}
	
	/**
	 * 跳转预约记录
	 * 
	 * @return
	 */
	@RequestMapping("/toReserveList.do")
	public ModelAndView toHighSeasPoolList(String userId, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) {
		Employee user = (Employee) session.getAttribute("loginUser");
		Map<String, Object> map = new HashMap<>();
		if ("".equals(userId) || userId == null) {
			userId = user.getEmployeeId();
		}
		map.put("personLiable", userId);
		List<Customer> list = customerService.getCustomerList(map);
		request.setAttribute("userId", userId);
		request.setAttribute("customer", list);
		return new ModelAndView("WEB-INF/jsp/reserve/reserveList");
	}

	/**
	 * 跳转员工预约记录
	 * 
	 * @return
	 */
	@RequestMapping("/toReserveEmployeeList.do")
	public ModelAndView toReserveEmployeeList(String userId, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		if (userId != null && !"".equals(userId)) {
			map.put("personLiable", userId);
			List<Customer> list = customerService.getCustomerList(map);
			request.setAttribute("userId", userId);
			request.setAttribute("customer", list);
			return new ModelAndView("WEB-INF/jsp/reserve/reserveEmployeeList");
		} else {
			return new ModelAndView("WEB-INF/jsp/error/404");
		}

	}

	/**
	 * 获取预约信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/reserveList.do")
	@ResponseBody
	public Map<String, Object> reserveList(String userId, String customerId, Integer limit, Integer page,
			String contractNum, String isCheck, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		Employee user = (Employee) session.getAttribute("loginUser");

		try {
			if (userId == null || "".equals(userId)) {
				userId = user.getEmployeeId();
			}
			param.put("employeeId", userId);// 所属用户条件
			param.put("customerName", customerId);// 所属客户条件
			param.put("page", page);// 当前页
			param.put("pageSize", limit);// 数量
			PageInfo<Reserve> pageInfo = reserveService.getReserveList(param);
			json.put("total", pageInfo.getPageCount());
			json.put("data", pageInfo.getList());
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 添加预约信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertReserve.do")
	@ResponseBody
	public Map<String, Object> insertActivity(Reserve reserve, String date1, String date2, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			// 格式化时间
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			if (!"".equals(date1) && date1!=null) {
				reserve.setReserveDate(formatter.parse(date1));
			}
			String reserveId=UUID.randomUUID().toString();
			reserve.setReserveId(reserveId);
			reserve.setCreateDate(new Date());// 写入创建时间
			reserve.setReserveStatus("0");// 写入记录状态
			reserve.setReserveEmployeeId(user.getEmployeeId());// 写入预约人
			if (reserveService.addReserve(reserve) > 0) {// 预约成功
				json.put("code", 0);
			}
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 修改预约信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateReserve.do")
	@ResponseBody
	public Map<String, Object> updateReserve(Reserve reserve, String date1, String date2, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			// 格式化时间
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			if (!"".equals(date1)  && date1!=null) {
				reserve.setReserveDate(formatter.parse(date1));
			}
			if (reserveService.updateReserve(reserve) > 0) {// 修改成功
				json.put("code", 0);
			}
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 获取预约信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getReserve.do")
	@ResponseBody
	public Map<String, Object> getReserve(String reserveId, HttpServletRequest request, HttpSession session,
			HttpServletResponse response){
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			Reserve reserve = reserveService.getReserveById(reserveId);
			json.put("reserve", reserve);
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 删除预约信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteReserve.do")
	@ResponseBody
	public Map<String, Object> deleteReserve(String reserveId, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			reserveService.deleteReserveById(reserveId);
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
}
